from google.appengine.ext import webapp
from google.appengine.ext.webapp import util

from ndb.model import *
from ndb.query import *
from ndb import model

from google.appengine.ext import db
from google.appengine.datastore import datastore_query

import time
def timeit(method):

    def timed(*args, **kw):
        ts = time.time()
        result = method(*args, **kw)
        te = time.time()
 
        print 'Running time = ' + '%2.2f sec' %( te-ts) + '<br />\n'     
        return result
    return timed
    

class H(db.Model):
  name = db.StringProperty()
  age = db.IntegerProperty()
  

class M(model.Model):
  name = model.StringProperty()
  age = model.IntegerProperty()

class RQ0(webapp.RequestHandler):
 @timeit 
 def get(self):
    values = range(10)
    q = M.query(M.age.IN(values)).order(M.key)
    ms = q.fetch(batch_size=1000, prefetch_size=1000)  # <------------
    print "="*40   
    self.response.out.write('Got %d results' % len(ms))
    
class RQ(webapp.RequestHandler):
 @timeit 
 def get(self):
    if self.request.get('i'):
     self.response.out.write('Initializing')
     ms = [M(id= str(i),name=str(i), age=i % 10) for i in range(1000)]
     put_multi(ms)
     return
     
    if self.request.get('d'):
      self.response.out.write('Delete')
      values = range(10)
      q = M.query(M.age.IN(values)).order(M.age, M.key)
      ms = q.fetch(batch_size=25, prefetch_size=25)  # <------------
      model.delete_multi(m.key for m in ms)
      return
      
    if self.request.get('g'):
      self.response.out.write('get');
      def getKeyFromID(id):
        k = model.Key('M', str(id))
        return k
        
      list_Account =  map(getKeyFromID,range(1000));
      print list_Account
      ms = model.get_multi(list_Account);
      
         
      self.response.out.write('Got %d results' % len(ms))
      return
      
      
    values = range(10)
    q = M.query(M.age.IN(values)).order(M.age, M.key)
    ms = q.fetch(batch_size=1000, prefetch_size=1000)  # <------------
    print "="*40   
    self.response.out.write('Got %d results' % len(ms))

    
class RQ2(webapp.RequestHandler):
 @timeit 
 def get(self):
    if self.request.get('i'):
     self.response.out.write('Initializing')
     ms = [H(key_name= str(i),name=str(i), age=i % 10) for i in range(1000)]
     db.put(ms)
     return
     
    values = range(10)
    
    qry = db.GqlQuery("SELECT * FROM H WHERE age = :1")
    
    results ={}
  
    def getMessage(age):
      qry.bind(age)
      results[age] = qry.run()
 
    map(getMessage,values) 
    
    ms = []
    for age in values:
      for h in results[age]:
        ms.append(h)
        
    print "="*40  
    self.response.out.write('Got %d results' % len(ms))
    
class RQ3(webapp.RequestHandler):
 @timeit 
 def get(self):
    values = range(10)
    
    qry = db.GqlQuery("SELECT * FROM H WHERE age = :1")
    
    results ={}
    
    if self.request.get('bz'):
      bz=int(self.request.get('bz'))
    else:
      bz=1000
  
    def getMessage(age):
      qry.bind(age)
      results[age] = qry.run(config=datastore_query.QueryOptions(batch_size=bz))
 
    map(getMessage,values) 
    
    ms = []
    for age in values:
      for h in results[age]:
        ms.append(h)
        
    print "="*40  
    self.response.out.write('Got %d results' % len(ms))
    
class RQ4(webapp.RequestHandler):
 @timeit 
 def get(self):
        
    values = range(10)
    
    ms = [];
    # qry = db.GqlQuery("SELECT * FROM H WHERE age IN" + '('+str(values)[1:-1] +')').run(config=datastore_query.QueryOptions(batch_size=1000))    
    str_values = '(0,1,2,3,4,5,6,7,8,9)'
    qry = db.GqlQuery("SELECT * FROM H WHERE age IN" + str_values).run(config=datastore_query.QueryOptions(batch_size=1000))    
    # ms = qry.fetch(1000)
    
    for h in qry:
      ms.append(h)
        
    print "="*40  
    self.response.out.write('Got %d results' % len(ms))       
    
urls = [
 ('/rq/rq0/.*', RQ0),
 ('/rq/rq1/.*', RQ),
 ('/rq/rq2/.*', RQ2),
 ('/rq/rq3/.*', RQ3),
 ('/rq/rq4/.*', RQ4)
 ]

app = webapp.WSGIApplication(urls)

def main():
 util.run_wsgi_app(app)

if __name__ == '__main__':
 main()
